import { defineStore } from 'pinia';
import { loginApi, getUserInfoApi } from '@/api/user';
import { getToken, removeToken, setToken } from '@/utils/token';
import router from '@/router';
import { useRoute } from 'vue-router';
export const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken() || '',
    userInfo: {}
  }),
  actions: {
    // 获取用户信息
    async getUserInfo() {
      try {
        const { data } = await getUserInfoApi()
        this.userInfo = data
      } catch (e) { }
    },
    // 设置token
    setToken(token) {
      this.token = token
      setToken(token)
    },
    // 登录
    async login(loginForm) {
      try {
        const result = await loginApi(loginForm)
        // 设置 token
        this.setToken(result.data.token)
      } catch (e) { }
    },
    // 登出
    logout() {
      this.token = ''
      this.userInfo = {}
      removeToken()
    }
  }
})